package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 加载首页-加载更多-加载更新 三位一体
     *
     * @param dto  type=1 认为是加载更多，type=2表示加载更新
     * @param type
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //1.非空判断
        if (dto.getSize() == 0) {
            dto.setSize(10);
        }
        if (dto.getTag() == null) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        //2. 直接调用sql,执行查询(判断类型，排序)
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, type);
        //3.返回数据
        return ResponseResult.okResult(apArticles);


    }

    /**
     * 保存三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        //TODO
        ApArticle apArticle = this.getById(dto.getId());
        //判断是否有这个文章
        if (ObjectUtil.isNotEmpty(apArticle)) {
            //没有就保存文章配置信息
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            if (ObjectUtil.isEmpty(apArticleConfig)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            articleConfigMapper.insert(apArticleConfig);
        }
        ApArticle bean = BeanUtil.toBean(dto, ApArticle.class);
        //保存或修改文章
        this.saveOrUpdate(bean);
        //保存文章内容
        ApArticleContent apArticleContent = new ApArticleContent();
          //文章的id
        apArticleContent.setArticleId(dto.getId());
          //文章的内容
        apArticleContent.setContent(dto.getContent());
        if (ObjectUtil.isEmpty(apArticleContent)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文章没有找到");
        }
        articleContentMapper.insert(apArticleContent);

        //生成rabbitmq消息唯一码
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        rabbitTemplate.convertAndSend(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, ArticleConstants.ARTICLE_ES_SYNC_ROUTINGKEY, apArticle.getId(), correlationData);
        //3.返回数据,要不要返回文章id ，因为wmnews表中需要
        return ResponseResult.okResult(dto.getId());
    }

}
